package com.chenjian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chenjian.common.CommonVO;
import com.chenjian.common.enums.RESPONSE_ENUM;
import com.chenjian.common.utils.BindResultUtils;
import com.chenjian.entity.Module;
import com.chenjian.entity.Sources;
import com.chenjian.entity.User;
import com.chenjian.exceptiom.CommonException;
import com.chenjian.service.ModuleService;
import com.chenjian.service.SourcesService;
import com.chenjian.service.UserService;
import com.chenjian.vo.ModuleSourcesVo;
import com.chenjian.vo.SourcesAddVo;
import com.chenjian.vo.UserModuleVo;
import com.google.common.collect.Lists;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/sources")
public class SourcesController {

    @Autowired
    private UserService userServiceImpl;

    @Autowired
    private SourcesService sourcesServiceImpl;

    @Autowired
    private ModuleService moduleServiceImpl;

    @PostMapping(value = "/add")
    public Object sourcesAdd(@RequestBody @Validated SourcesAddVo sourcesAddVo, BindingResult bindingResult) {

        BindResultUtils.checkBindingResult(bindingResult);

        Module module = moduleServiceImpl.getById(sourcesAddVo.getModuleId());
        if (null != module) {
            Sources sources = new Sources();
            sources.setModuleId(module.getId());
            sources.setSourcesName(sourcesAddVo.getSourcesName());
            sources.setSourcesInfo(sourcesAddVo.getSourcesInfo());
            sourcesServiceImpl.save(sources);
            return new CommonVO<>(sources);
        }


        throw new CommonException("模块数据异常", RESPONSE_ENUM.FAIL_404.getCode());
    }

    @GetMapping(value = "/find/sources/by/user/{id}")
    public Object getUserSources(@PathVariable Long id) {

        User user = userServiceImpl.getById(id);
        if (null != user) {
            QueryWrapper<Module> wrapper = Wrappers.query();
            wrapper.eq("user_id", user.getId());
            List<Module> modules = moduleServiceImpl.list(wrapper);
            UserModuleVo userModuleVo = new UserModuleVo();
            ArrayList<ModuleSourcesVo> list = Lists.newArrayList();
            for (Module module : modules) {
                ModuleSourcesVo moduleSourcesVo = new ModuleSourcesVo();
                moduleSourcesVo.setModule(module);
                QueryWrapper<Sources> wrapper1 = Wrappers.query();
                wrapper1.eq("module_id", module.getId());
                List<Sources> sourcesList = sourcesServiceImpl.list(wrapper1);
                moduleSourcesVo.setSourcesList(sourcesList);
                list.add(moduleSourcesVo);
            }
            userModuleVo.setUser(user);
            userModuleVo.setModuleSourcesVoList(list);
            return new CommonVO<>(userModuleVo);
        }
        throw new CommonException("用户信息异常", RESPONSE_ENUM.FAIL_404.getCode());
    }

    @GetMapping(value = "/find/sources/by/user/v2/{id}")
    public Object getUserSources2(@PathVariable Long id) {
        List<Sources> sourcesList = sourcesServiceImpl.getSourcesByUserId(id);
        return new CommonVO<>(sourcesList);
    }
}
